package ar.edu.unlu.sistemas.movirdroid.android;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import ar.edu.unlu.sistemas.movirdroid.business.util.Constants;
import ar.edu.unlu.sistemas.movirdroid.android.R;

public class MainActivity extends AbsP2PActivity {
	
	private ProgressDialog dialog;
	
	 // For getting confirmation from the service
    private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (dialog.isShowing())
            	dialog.dismiss();

            // Change activity
            Intent intentActivity = new Intent(MainActivity.this,
                    QueryActivity.class);
            startActivity(intentActivity);
         
            finish();
        }
    };
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        dialog = ProgressDialog.show(this, "", 
        "Cargando. Por favor espere...");
        // Register broadcastReceiver to know when the service finished
        // its creation
        this.registerReceiver(serviceReceiver,new IntentFilter(Constants.BOOT_COMPLETE_INTENT));
    }

    @Override
	protected void onStart() {
		super.onStart();
		Intent serviceIntent = new Intent(MainActivity.this,AppService.class);
        serviceIntent.putExtra(Constants.PORT, "3600");
        serviceIntent.putExtra(Constants.ALIAS, "Pepo");
        startService(serviceIntent);
        bindService(new Intent(MainActivity.this,AppService.class), mConnection, BIND_AUTO_CREATE);
	}
    
}